package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.EventBusBaseBean1;
import org.gangcai.mac.shop.bean.PullAuthoBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.oberver.CommonObserver;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AuthoContactsActivity extends BaseAuthoActivity {

    @BindView(R.id.father_name)
    EditText fatherName;
    @BindView(R.id.father_mobile)
    EditText fatherMobile;
    @BindView(R.id.mother_name)
    EditText motherName;
    @BindView(R.id.mother_mobile)
    EditText motherMobile;
    @BindView(R.id.instructor_name)
    EditText instructorName;
    @BindView(R.id.instructor_mobile)
    EditText instructorMobile;
    @BindView(R.id.roommate_name)
    EditText roommateName;
    @BindView(R.id.roommate_mobile)
    EditText roommateMobile;
    @BindView(R.id.classmate_name)
    EditText classmateName;
    @BindView(R.id.classmate_mobile)
    EditText classmateMobile;
    @BindView(R.id.nextStep)
    Button nextStep;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).statusBarDarkFont(true).keyboardEnable(true).init();
        ButterKnife.bind(this);
        getAuthoData("getcontacts").subscribe(new CommonObserver<PullAuthoBean>() {
            @Override
            public void onSuccess(PullAuthoBean value) {
                PullAuthoBean.InfoBean info = value.getInfo();
                fatherName.setText(info.getFather_name());
                fatherMobile.setText(info.getFather_mobile());
                motherMobile.setText(info.getMother_mobile());
                motherName.setText(info.getMother_name());
                instructorName.setText(info.getInstructor_name());
                instructorMobile.setText(info.getInstructor_mobile());
                roommateName.setText(info.getRoommate_name());
                roommateMobile.setText(info.getRoommate_mobile());
                classmateMobile.setText(info.getClassmate_mobile());
                classmateName.setText(info.getClassmate_name());


            }
        });
    }

    @Override
    protected String getTitleName() {
        return "联系人";
    }

    @Override
    public int getcontentView() {
        return R.layout.activity_autho_contacts;
    }

    @OnClick(R.id.nextStep)
    public void onViewClicked() {
        if (TextUtils.isEmpty(fatherName.getText().toString())) {
            ToastUtils.showShort("请输入父亲的姓名");
        } else if (!RegexUtils.isMobileExact(fatherMobile.getText().toString())) {
            ToastUtils.showShort("请准确的输入您的父亲的手机号");
        } else if (TextUtils.isEmpty(motherName.getText().toString())) {
            ToastUtils.showShort("请输入母亲的姓名");
        }  else if (!RegexUtils.isMobileExact(motherMobile.getText().toString())) {
            ToastUtils.showShort("请准确的输入您的母亲的手机号");
        } else if (TextUtils.isEmpty(instructorName.getText().toString())) {
            ToastUtils.showShort("请输入导员的姓名");
        }  else if (!RegexUtils.isMobileExact(instructorMobile.getText().toString())) {
            ToastUtils.showShort("请准确的输入您的导员的手机号");
        }  else if (!RegexUtils.isMobileExact(roommateMobile.getText().toString())) {
            ToastUtils.showShort("请准确的输入您的室友的手机号");
        } else if (TextUtils.isEmpty(roommateName.getText().toString())) {
            ToastUtils.showShort("请输入室友的姓名");
        } else if (TextUtils.isEmpty(classmateName.getText().toString())) {
            ToastUtils.showShort("请输入同学的姓名");
        } else if (!RegexUtils.isMobileExact(classmateMobile.getText().toString())) {
            ToastUtils.showShort("请准确的输入您的同学的手机号");
        } else {
            submitData();
        }
    }

    private void submitData() {
        prompDialog.showLoading("请等待");
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("uid", SPUtils.getInstance().getString("uid"));
        paramMap.put("father_name", fatherName.getText().toString());
        paramMap.put("father_mobile", fatherMobile.getText().toString());
        paramMap.put("mother_name", motherName.getText().toString());
        paramMap.put("mother_mobile", motherMobile.getText().toString());
        paramMap.put("instructor_name", instructorName.getText().toString());
        paramMap.put("instructor_mobile", instructorMobile.getText().toString());
        paramMap.put("roommate_name", roommateName.getText().toString());
        paramMap.put("roommate_mobile", roommateMobile.getText().toString());
        paramMap.put("classmate_name", classmateName.getText().toString());
        paramMap.put("classmate_mobile", classmateMobile.getText().toString());
        submitDataToServer("contacts_ident", paramMap, null)
                .subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            showMsg("已提交");
                            EventBus.getDefault().postSticky(new EventBusBaseBean1());
                            finish();
                        }
                    }
                });

    }
}
